package com.lightbook.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lightbook.pojo.Cart;
import com.lightbook.result.Result;
import com.lightbook.service.CartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "购物车管理")
@Controller
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService cartService;

    @GetMapping("")
    public String cart(@CookieValue(defaultValue ="sss") String userEmail, Model model) {

        if(userEmail.equals("sss")){
            model.addAttribute("fail", "请输入账号和密码");
            return "login";
        }else {
            return "cart2";
        }
    }

    @ResponseBody
    @GetMapping("/jsontest")
    public List<Cart> jsonTest() {

        List<Cart> list = cartService.list(null);
        return list;
    }

    //    条件分页查询
//    page 当前页 limit每页显示条数
    @ResponseBody
    @GetMapping("/table/{page}/{limit}")
    public Map<String, Object> dynamic_table(@PathVariable Long page,
                                             @PathVariable Long limit,
                                             @CookieValue String userEmail){
        //查询所有数据
        // List<User> list = userService.list();
        //model.addAttribute("users",list);
        //分页查询数据

        Page<Cart> cartPage = new Page<Cart>(page, limit);//pn是当前页码，5为每页显示的数据条数
//        Page<Cart> pages= cartService.page(cartPage, new QueryWrapper<Cart>().orderByDesc("cart_id"));
        IPage<Cart> pages = cartService.selectPage(cartPage, userEmail);// 分页查询的结果
//     按邮箱查询
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_email",userEmail);
        List<Cart> list = cartService.list(wrapper);
//        List<Cart> list = cartService.list();
//        String cart1 = pages.getRecords().get(6).getUserEmail();

        //        商品记录数
        Integer cartTotal1=0;
//        商品总价格
        double cartTotal2=0;

        for(Cart cart:list)
        {
            cartTotal1+=cart.getCartCount();
            cartTotal2+=cart.getCartTotalPrice();
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("cartTotal1",cartTotal1);
        map.put("cartTotal2",cartTotal2);
        map.put("pages",pages);
        System.out.println(userEmail);
        return map;
    }

    @ApiOperation("分页查询")
    @ResponseBody
    @GetMapping("/search/{page}/{limit}")
    public Result getListByKeyword(@PathVariable Long page,
                                   @PathVariable Long limit,
                                   String keyword) {
        Page<Cart> pageNum = new Page<>(page, limit);
        IPage<Cart> iPage = cartService.selectPage(pageNum, keyword);
        return Result.ok(iPage);
    }

    @ResponseBody
    @GetMapping("/updata")
    public void updata( Integer cartId, Integer cartCount){

//        条件查询
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("cart_id",cartId);
        List<Cart> list = cartService.list(wrapper);

        Cart cart = new Cart();
        cart=list.get(0);
        System.out.println(cart);
        cart.setCartTotalPrice(cart.getCartPrice()*cartCount);
        cart.setCartCount(cartCount);
//        根据id修改
        cartService.updateById(cart);
    }

//    根据id删除
    @ResponseBody
    @GetMapping("/delete")
    public void delete( Integer cartId){
        System.out.println(cartId);
        cartService.removeById(cartId);
    }
    //    删除所有
    @ApiOperation("删除所有订单")
    @ResponseBody
    @GetMapping("/deleteAll")
    public void deleteALL(@CookieValue String userEmail){
        //     按邮箱查询
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_email",userEmail);
        List<Cart> list = cartService.list(wrapper);
        for (Cart cart:list)
        {
            cartService.removeById(cart.getCartId());
        }
    }


}
